// -----------------------------
// Author: 		Dhaenens Gert	
// Creation: 	2/26/2015 6:01:53 PM				
// -----------------------------

#include "File.h"

#include "FilePackage.h"

namespace Flow
{

	File::File(FilePackage* lpPackage, const tstring& fileName, UInt64 dataStart, UInt64 dataSize)
		: m_lpPackage(lpPackage)
		, m_FileName(fileName)
		, m_DataStart(dataStart)
		, m_DataSize(dataSize)
		, m_IsOpen(false)
	{
	}

	File::~File()
	{
	}

	void File::Open()
	{
		m_IsOpen = m_lpPackage->OpenPackageFile();
		ASSERT(m_IsOpen);
		
		m_lpPackage->SetPosition((Size_T) m_DataStart);		
	}
	void File::Close()
	{
		m_lpPackage->ClosePackageFile();
		m_IsOpen = false;
	}

	void* File::ReadAllData()
	{
		void* lpData = malloc((Size_T) m_DataSize);
		ASSERT(lpData);

		ReadAllData(lpData);

		return lpData;
	}
	void File::ReadAllData(void* lpDataOutput)
	{
		Open();
		m_lpPackage->SetPosition((Size_T) m_DataStart);
		m_lpPackage->Read(lpDataOutput, (Size_T) m_DataSize);
		Close();
	}

	void File::Read(void* lpData, Size_T dataSize) const
	{
		m_lpPackage->Read(lpData, dataSize);
	}

	void File::SetPosition(Size_T position) const
	{
		m_lpPackage->SetPosition((Size_T) m_DataStart + position);
	}
	void File::ResetPosition() const
	{
		m_lpPackage->SetPosition((Size_T) m_DataStart);
	}

	FilePackage* File::GetPackage() const
	{
		return m_lpPackage;
	}
	const tstring& File::GetName() const
	{
		return m_FileName;
	}
	UInt64 File::GetDataStart() const
	{
		return m_DataStart;
	}
	UInt64 File::GetDataSize() const
	{
		return m_DataSize;
	}

}